﻿

namespace Huirui.Cavan.Presentation.Controller.SystemModule
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Mvc;
    using Huirui.Cavan.Core.Extension;
    using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
    using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
    using Huirui.Cavan.Presentation.Seedwork;
    using Huirui.Cavan.Presentation.ViewModel.SystemModule;
    using Huirui.Cavan.Application.MainBoundedContext.SystemModule;

    public class AreaController : EntityController<Area, string, AreaSearch, IAreaViewModel, IAreaAppService>, IAreaController
    {
        public override string ControllerName
        {
            get { return ControllerConst.AREA; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public AreaController()
        {
            this.Model = ModelFactory.CreateModel<IAreaViewModel>();
        }

        [ActionName(ActionConst.SELECTAREA)]
        public ActionResult GetArea(string provinceID, string cityID, string purpose)
        {
            GetAreaList(this.Model, provinceID, cityID);
            this.Model.Purpose = purpose;
            return PartialView("~/Views/System/v1/selectarea.cshtml", this.Model);
        }

        [ActionName(ActionConst.SELECTCITY)]
        public JsonResult GetCity(string id)
        {
            //SetCityItem(this.Model, id);
            GetAreaList(this.Model, id, string.Empty);
            return Json(this.Model);
        }

        public void GetAreaList(IAreaViewModel areaViewModel, string provinceID, string cityID)
        {
            this.AppService.InitAreaList();
            if (this.Model.Provinces.IsNull() || this.Model.Provinces.Count == 0)
            {
                List<SelectListItem> provinceItems = new List<SelectListItem>();
                provinceItems.Add(new SelectListItem { Text = "请选择", Value = "0", Selected = provinceID.IsNotEmpty() ? false : true });
                foreach (Area a in Area.Provinces)
                {
                    provinceItems.Add(new SelectListItem { Text = a.Name, Value = a.Id, Selected = a.Id == provinceID ? true : false });
                }
                this.Model.Provinces = provinceItems;
            }

            List<SelectListItem> cityItems = new List<SelectListItem>();
            cityItems.Add(new SelectListItem { Text = "请选择", Value = "0", Selected = false });
            if (provinceID.IsNotEmpty() && !provinceID.Equals("0"))
            {
                foreach (Area a in Area.Cities.Where(r => r.ParentCode == provinceID).ToList())
                {
                    cityItems.Add(new SelectListItem { Text = a.Name, Value = a.Id });
                }
            }
            this.Model.Cities = cityItems;

            if (cityID.IsNotEmpty() && !cityID.Equals("0"))
            {
                SelectListItem slCity = this.Model.Cities.FirstOrDefault(t => t.Value == cityID);
                if (slCity.IsNotNull())
                {
                    slCity.Selected = true;
                }
                else
                {
                    this.Model.Cities.FirstOrDefault().Selected = true;
                }
            }
            else
            {
                this.Model.Cities.FirstOrDefault().Selected = true;
            }
        }

        //private void SetCityItem(IAreaViewModel areaViewModel, string parentID)
        //{
        //    List<SelectListItem> cityItems = new List<SelectListItem>();
        //    cityItems.Add(new SelectListItem { Text = "请选择", Value = "0", Selected = false });
        //    if (parentID.IsNotEmpty() && parentID != "0")
        //    {
        //        AreaSearch areaSearch = SearchFactory.CreateAreaSearch(parentID, 2, true);
        //        IList<Area> citys = this.AppService.GetBySearch(areaSearch).ToList();
        //        foreach (Area a in citys)
        //        {
        //            cityItems.Add(new SelectListItem { Text = a.Name, Value = a.Id });
        //        }
        //    }

        //    areaViewModel.Cities = cityItems;
        //}
    }
}
